package com.scpii.universal.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.scpii.universal.hibernate.dao.BaseDao;
import com.scpii.universal.hibernate.entity.AttributeDto;
import com.scpii.universal.hibernate.entity.AttributeValueTab;
import com.scpii.universal.hibernate.intface.BaseInterface;

public class AttributeValueTabService implements
		BaseInterface<AttributeValueTab> {
	private static final Logger log = LoggerFactory
			.getLogger(AttributeValueTabService.class);
//	private BaseDao<AttributeValueTab> attributeValueTabDao = SpringBeanFactory.attributeValueTabDao;
	private BaseDao<?> attributeValueTabDao;
	
	public AttributeValueTab get(Integer id) throws Exception {
		return (AttributeValueTab) attributeValueTabDao.get(id);
	}

	public List<AttributeValueTab> getAll() throws Exception {
		return (List<AttributeValueTab>) attributeValueTabDao.getAll();
	}

	public AttributeValueTab get(String propertyName, Object value)
			throws Exception {
		return null;
	}

	public List<AttributeValueTab> getList(String propertyName, Object value)
			throws Exception {
		return null;
	}

	public Long getTotalCount() throws Exception {
		return null;
	}

	public void save(AttributeValueTab obj) throws Exception {

	}

	public void update(AttributeValueTab obj) throws Exception {

	}

	public void delete(AttributeValueTab obj) throws Exception {

	}

	public void delete(Integer id) throws Exception {

	}

	public List<AttributeValueTab> hqlGetList(String hql,
			Map<String, Object> argsMap) throws Exception {
		return (List<AttributeValueTab>) attributeValueTabDao.hqlGetList(hql, argsMap);
	}
	
	public List<AttributeDto> getAttrValueAndAttrInfo(int contentId) {
		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("contentId", contentId);
		return (List<AttributeDto>)attributeValueTabDao.hqlGetList("GET_ATTRVALUE_AND_ATTRINFO", argsMap);
	}

	public void hqlUpdate(String namedHqlQuery, Map<String, Object> argsMap)
			throws Exception {
		attributeValueTabDao.hqlUpdate(namedHqlQuery, argsMap);
	}

	public Long hqlGetNums(String namedHqlQuery, Map<String, Object> argsMap)
			throws Exception {
		return attributeValueTabDao.hqlGetNums(namedHqlQuery, argsMap);
	}

	public List<AttributeValueTab> sqlGetList(String sqls,
			Map<String, Object> argsMap) throws Exception {
		return (List<AttributeValueTab>) attributeValueTabDao.sqlGetList(sqls, argsMap);
	}

	public void sqlUpdate(String sqls, Map<String, Object> argsMap)
			throws Exception {
		attributeValueTabDao.sqlUpdate(sqls, argsMap);
	}

	public Long sqlGetNum(String sqls, Map<String, Object> argsMap)
			throws Exception {
		return attributeValueTabDao.sqlGetNum(sqls, argsMap);
	}

	/**
	 * 根据内容ID，获得与内容相关的属性值表中的记录
	 * 
	 * @param contentId
	 * @return
	 * @throws Exception
	 * @author 甘建新 2012-7-17上午12:54:18
	 */
	public List<AttributeValueTab> getAttributeValueListByContentId(
			int contentId) throws Exception {
		if (contentId > 0) {
			List<AttributeValueTab> attrValueList = new ArrayList<AttributeValueTab>();
			Map<String, Object> argsMap = new HashMap<String, Object>();
			argsMap.put("contentId", contentId);
			attrValueList = this.hqlGetList("GET_ATTRIBUTE_VALUES_CONTENTID",
					argsMap);
			return attrValueList;
		}
		return null;
	}
	
	/**
	 * 根据内容属性ID获取属性值
	 * @author Taylor
	 * 2012-9-16 上午01:29:48
	 * @param contentId
	 * @return
	 * @throws Exception
	 * AttributeValueTab
	 */
	public AttributeValueTab getAttributeValueByContAttrId(
			int contentId,int attrId) throws Exception {
		List<AttributeValueTab> attrValueList = new ArrayList<AttributeValueTab>();
		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("contentId", contentId);
		argsMap.put("intAttriId", attrId);
		attrValueList = this.hqlGetList("GET_ATTRIBUTE_VALUE_BY_CONTATTRID",
				argsMap);
		if (attrValueList.size() > 0) {
			return attrValueList.get(0);
		}
		return null;
	}
	
	/**
	 * 获得属性值Map
	 * @param contentId
	 * @return
	 * @throws Exception
	 * @author 甘建新
	 * 2012-7-25下午11:02:12
	 */
	public Map<Integer, String> getAttriIdValueMap(int contentId) throws Exception{
		Map<Integer, String> keyValueMap = new HashMap<Integer, String>();
		if (contentId>0) {
			List<AttributeValueTab> attrValueList = this.getAttributeValueListByContentId(contentId);
			if (attrValueList!=null && attrValueList.size()>0) {
				for (AttributeValueTab attributeValueTab : attrValueList) {
					if (attributeValueTab!=null) {
						keyValueMap.put(attributeValueTab.getIntAttriId(), attributeValueTab.getVarAttriValue());
					}
				}
			}
		}
		return keyValueMap;
	}
	//Setter

	public void setAttributeValueTabDao(
			BaseDao<AttributeValueTab> attributeValueTabDao) {
		this.attributeValueTabDao = attributeValueTabDao;
	}
	
}